<?php


/**
 * mt_append_body_class()
 * 
 * body class attribute
 * 
 * @return void
 * 
 * @since 0.1
 */
function mt_append_body_class(){
    echo Filter::apply('body_class','');
}

/**
 * mt_is_at()
 * 
 * check current location
 * 
 * @param string $slugname slugname i.e home, pagename
 * @return bool
 * 
 * @since 0.1
 */
function mt_is_at($slugname){
    return (MT_CURRENT_PAGE == $slugname);
}

/**
 * void mt_is_home()
 * 
 * @return bool
 * 
 * @since 0.1
 */
function mt_is_home(){    
    return ( MT_CURRENT_PAGE == 'home' );
}


function mt_menu(){
    $menu = new Table('menu');

            // Select all items
            $items = $menu->select(null, 'all', null, array('id', 'name', 'link', 'target', 'order'), 'order', 'ASC');
}


function b64_safe_encode($string){

        settype($string, "string");

        $output = base64_encode($string);

        $output = str_replace(array('+', '/', '='), array('-', '_', ''), $output);

        return $output;
}


function b64_safe_decode($string){

        settype($string, "string");

        $output = str_replace(array('-', '_'), array('+', '/'), $string);

        $mod4 = (strlen($output) % 4);

        if ($mod4)
        {
                $output .= substr('====', $mod4);
        }

        $output = base64_decode($output);

        return $output;
}

/**
 * HTML tag Helper function 
 * function _t()
 * 
 * example
 * <code>
 * echo _t('a','website',array('href'=>'http://blog.kaizeku.com'));
 * echo _t('script','',array('src'=>'http://path/tofile.ext'));
 * </code>
 * 
 * @param string        $tag html tagname; a p strong
 * @param mixed|string  $content the content 
 * @param mixed|array   $htmlattributes html tag attributes in pair key value
 * @return string
 * 
 * @see Html::chars
 * @since 0.1
 */
function _t($tag = 'a', $content = false, $htmlattributes = false)
{


        $tag = strtolower($tag);
        $htm = '<' . $tag;

        if (is_array($htmlattributes))
        {

                // add name || id for input
                if ($tag == 'input')
                {
                        if (isset($htmlattributes['id']))
                        {
                                $htmlattributes['name'] = $htmlattributes['id'];
                        } elseif (isset($htmlattributes['name']))
                        {
                                $htmlattributes['id'] = $htmlattributes['name'];
                        }

                }

                $esc_prop = array_flip(array('title', 'alt', 'caption', 'content'));

                foreach ($htmlattributes as $name => $txt)
                {

                        $name = strtolower($name);

                        if ($tag == 'blockquote')
                        {
                                if ($name == 'cite')
                                {
                                        $txt = urlencode($txt);
                                }
                        }

                        if (isset($esc_prop[$name]))
                        {
                                $htm .= ' ' . $name . '="' . Html::chars($txt) . '"';
                        } else
                        {
                                $htm .= ' ' . $name . '="' . $txt . '"';
                        }
                }


                unset($htmlattributes, $esc_prop, $name, $txt);
        }

        /**
         * @link http://dev.w3.org/html5/html-author/#void-elements
         */
        $typesingle = array_flip(array('img', 'input', 'hr', 'link', 'meta', 'br'));

        /**
         * HTML Compatibility Guideline 2
         * Include a space before the trailing /
         * {@link http://www.w3.org/TR/xhtml1/#C_2 Empty Elements}
         */
        $htm .= (isset($typesingle[$tag])) ? ' />' : '>' . $content . '</' . $tag . '>';

        /** 
         * WCAG AAA
         * newline for common BLOCK elements
         */
        $newline_tag = array_flip(array('h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'ul',
                'ol', 'dl', 'li', 'script', 'pre', 'code', 'div', 'form', 'table', 'dt', 'dd',
                'blockquote', 'meta', 'link', 'title'));

        if (isset($newline_tag[$tag]))
        {
                $htm .= PHP_EOL;
        }

        unset($typesingle, $newline_tag, $tag, $content);

        return $htm;
}
/**
 * @see _t
 */
function t($tag = 'a', $content = false, $htmlattributes = false)
{
        echo _t($tag, $content, $htmlattributes);
}

function mtGetForeachChunk(array $array){
    
    if (_has_count($array)){
        
        foreach($array as $k) {
            Chunk::get($k);
        }   
    }    
}

/**
 * void mt_foreach_do()
 * 
 * foreach user func 
 * 
 * @param mixed $array array list
 * @param string $callback function name
 * @return void
 * 
 * @since 0.1
 */
function mt_foreach_do(array $array, $callback){
    
    if (_has_count($array)){
        
        foreach($array as $arg) {
            call_user_func($callback, $arg);
        }   
    }
}

/**
 * _has_count()
 * 
 * @param mixed $array array list
 * @return bool
 * 
 * @since 0.1
 */ 
function _has_count($array){ return (count($array)>0); }

/**
 * void mt_q_css()
 * quee stylesheet to be minify
 * 
 * @param string $filename stylesheet filename without the *.css ext
 * @return void
 * 
 * @since 0.1
 */ 
function mt_q_css($filename){
    Stylesheet::add(MT_CSS_PATH.$filename.'.css');
}

/**
 * mt_get_relative_url()
 * this function will strip http/https & hostname 
 * return base url
 * 
 * @param string $url full qualified url
 * @return string base url
 * 
 * @since 0.1
 */
function mt_get_relative_url( $url ) {
	return preg_replace( '|https?://[^/]+(/.*)|i', '$1', $url );
}

/**
 * Debug helper function.  This is a wrapper for var_dump() that adds
 * the <pre /> tags, cleans up newlines and indents, and runs
 * htmlentities() before output.
 *
 * @param mixed $var     The variable to dump.
 * @param string $label  Label to prepend to output.
 * @param boolean $print Print the output if true.
 * @param boolean $exit  Exit after echoing if true
 * @return string
 * 
 * @author andris
 * @link http://codeaid.net/php/improved-zend_debug::dump%28%29-and-var_dump%28%29-functions
 * @since 0.1
 */
function mt_dump($var, $label = null, $print = true, $exit = false)
{
    
    // format the label
    $label_text = $label;
    $label = ($label === null) ? '' : '<h3 style="margin: 0px">' . trim($label) . '</h3>';
 
    // var_dump the variable into a buffer and keep the output
    ob_start();
    var_dump($var);
    $output = ob_get_clean();
 
    // neaten the newlines and indents
    $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
 
    if (is_array($var)) {
        $keys = array_keys_multi($var);
        $maxlen = 0;
 
        // determine the number of characters in the longest key
        foreach ($keys as $key) {
            $len = strlen($key);
            if ($len > $maxlen) {
                $maxlen = $len;
            }
        }
 
        // account for [" and "]
        $maxlen += 4;
 
        // append spaces between "] and =>
        $output = preg_replace_callback('/\[.*\]/', create_function('$matches', 'return str_pad($matches[0], ' . $maxlen . ');'), $output);
    }
 
    if (PHP_SAPI == 'cli') {
        $output = PHP_EOL . $label_text
                . PHP_EOL . $output
                . PHP_EOL;
    } else {
        if (!extension_loaded('xdebug')) {
            $output = htmlspecialchars($output, ENT_QUOTES);
        }
 
        $output = '<pre style="font-family: \'DejaVu Sans Mono\',\'Courier New\'; font-size: 12px; background-color: #FBFED7; margin: 4em auto; padding: 10px; border: 1px solid #CCCCCC; max-width: 1000px;width:80%">'
                . $label
                . $output
                . '</pre>';
    }
 
    if ($print === true) {
        print $output;
    }
 
    if ($exit === true) {
        exit;
    }
 
    return $output;
}
?>